
import Sprite from './sprite'

const screenWidth = window.innerWidth
const screenHeight = window.innerHeight

/**
 * 背景
 */
export default class BackGround extends Sprite{

  constructor() {
    super('images/bg.jpg', 512, 512)
    this.top = 0
  }

  update() {
    this.top += 2
    if (this.top >= screenHeight){
      this.top = 0
    }
  }
  /**
   * 两张图片交替，实现背景滚动
   */
  render(ctx) {
    ctx.drawImage(
      this.img, 0, 0 , this.width, this.height, 0, this.top , screenWidth, screenHeight
    )
    ctx.drawImage(
      this.img, 0, 0, this.width, this.height, 0, this.top - screenHeight, screenWidth, screenHeight
    )
  }
}
